package com.smarthome.core.workingmonitor.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShEquipmentDevCameraService;
import com.smarthome.base.service.ShEquipmentDevIpcService;
import com.smarthome.base.service.ShEquipmentDevSwingService;
import com.smarthome.base.service.ShEquipmentDevTalkService;
import com.smarthome.base.service.ShEquipmentStatusService;
import com.smarthome.base.service.ShEquipmentTypeService;
import com.smarthome.common.tools.IPAliveUtils;
import com.smarthome.core.deviceManager.EquipmentProducerService;
import com.smarthome.core.dictionary.DeviceTypeService;
import com.smarthome.core.workingmonitor.DeviceStateService;
import com.smarthome.domain.bean.ShEquipmentDevCamera;
import com.smarthome.domain.bean.ShEquipmentDevIpc;
import com.smarthome.domain.bean.ShEquipmentDevSwing;
import com.smarthome.domain.bean.ShEquipmentDevTalk;
import com.smarthome.domain.bean.ShEquipmentStatus;
import com.smarthome.domain.bean.ShEquipmentType;
import com.smarthome.domain.enums.EquipmentStatusEnum;
import com.smarthome.domain.enums.EquipmentTypeEnum;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;


@Service("deviceStateService")
public class DeviceStateServiceImpl implements DeviceStateService {

	protected static Logger LOGGER;

    @Resource
    private ShEquipmentStatusService shEquipmentStatusService;
    
    @Resource
    private DeviceTypeService deviceTypeService;
    
    
    @Resource
    private EquipmentProducerService equipmentProducerService;
    
    @Resource
    private ShEquipmentDevCameraService shEquipmentDevCameraService;
    
    @Resource
    private ShEquipmentDevIpcService shEquipmentDevIpcService;
    
    @Resource
    private ShEquipmentDevSwingService shEquipmentDevSwingService;
    
    @Resource
    private ShEquipmentDevTalkService shEquipmentDevTalkService;
    
    @Resource
    private ShEquipmentTypeService shEquipmentTypeService;

    
	
    /*@Override
	public void getAopuIpcResidenceDoorStatus(AopuIpcResidenceDoorStatusRequest request) {
		//一个小区只有一个奥普设备，在我们设备表有多少个摆闸有多少条记录。
		//设备ID,在我们的后台表去查有多少个摆闸。
		
		ShEquipmentInfo IPC = new ShEquipmentInfo();
		IPC.setEquipmentTypeCode(EquipmentTypeEnum.DEV_IPC.getCode());
		IPC.setSelfCode(request.getIpcId());
		List<ShEquipmentInfo> IPCList = equipmentInfoService.queryShEquipmentInfoList(IPC);
		
		ShEquipmentProducer equipmentProducer = new ShEquipmentProducer();
		equipmentProducer.setProBzCode(EquipmentProduEnum.DEV_PRO_AOPU.getCode());
		List<ShEquipmentProducer> equipmentProducers = equipmentProducerService.queryShEquipmentProducerList(equipmentProducer);
		Long produerId = equipmentProducers.get(0).getId();
		
		ShEquipmentInfo equipmentInfo = new ShEquipmentInfo();
		equipmentInfo.setEquipmentTypeCode(EquipmentTypeEnum.DEV_SWING.getCode());
		equipmentInfo.setParentDevId(String.valueOf(IPCList.get(0).getId()));
		equipmentInfo.setProduerId(produerId);
		
		List<ShEquipmentInfo> list = equipmentInfoService.queryShEquipmentInfoList(equipmentInfo);
		int length = list.size();
		ShEquipmentStatus equipmentStatus = null;
		ShEquipmentStatus[] equipmentStatusArr = new ShEquipmentStatus[list.size()];
		for(int i = 0;i < length;i++) {
			equipmentStatus = new ShEquipmentStatus();
			boolean flag = request.getDoorss().get(list.get(i).getSelfCode());
			equipmentStatus.setEquipmentInfoId(list.get(i).getId());
			equipmentStatus.setGmtCreate(new Date());
			//保存设备状态
			equipmentStatus.setStatus(flag?EquipmentStatusEnum.AVAILABLE.getCode():EquipmentStatusEnum.NOAVAILABLE.getCode());
			equipmentStatusArr[i] = equipmentStatus;
		}
		shEquipmentStatusService.insert(equipmentStatusArr);
	}*/

	/*@Override
	public void getHKIpcResidenceDoorStatus(HKIpcResidenceDoorStatusRequest request) {
		ShEquipmentInfo IPC = new ShEquipmentInfo();
		IPC.setEquipmentTypeCode(EquipmentTypeEnum.DEV_IPC.getCode());
		IPC.setSelfCode(request.getIpcId());
		List<ShEquipmentInfo> IPCList = equipmentInfoService.queryShEquipmentInfoList(IPC);
		
		
		ShEquipmentProducer equipmentProducer = new ShEquipmentProducer();
		equipmentProducer.setProBzCode(EquipmentProduEnum.DEV_PRO_HK.getCode());
		List<ShEquipmentProducer> equipmentProducers = equipmentProducerService.queryShEquipmentProducerList(equipmentProducer);
		Long producerId = equipmentProducers.get(0).getId();
		
		ShEquipmentInfo equipmentInfo = new ShEquipmentInfo();
		equipmentInfo.setEquipmentTypeCode(EquipmentTypeEnum.DEV_TALK.getCode());
		equipmentInfo.setParentDevId(String.valueOf(IPCList.get(0).getId()));
		equipmentInfo.setProduerId(producerId);
		equipmentInfo.setSelfCode(request.getDeviceId());
		List<ShEquipmentInfo> infos = equipmentInfoService.queryShEquipmentInfoList(equipmentInfo);
		
		ShEquipmentStatus equipmentStatus = new ShEquipmentStatus();
		equipmentStatus.setStatus("0".equals(request.getStatus())?EquipmentStatusEnum.AVAILABLE.getCode():EquipmentStatusEnum.NOAVAILABLE.getCode());
		equipmentStatus.setEquipmentInfoId(infos.get(0).getId());
		equipmentStatus.setGmtCreate(new Date());
		shEquipmentStatusService.insert(equipmentStatus);
	}*/

	@Override
	public DataGrid dataGrid(ShEquipmentStatus equipmentStatus, PageHelper page) {
		return shEquipmentStatusService.queryShEquipmentStatus(equipmentStatus, page);
	}

	@Override
	public List<Combobox> equipmentTypeCombobox() {
        return deviceTypeService.getDeviceTypeCombobox(false);
	}

	@Override
	public List<Combobox> statusCombobox() {
		EquipmentStatusEnum[] statuses = EquipmentStatusEnum.values();
		List<Combobox> result = new ArrayList<>();
		Combobox combobox = null;
		for(EquipmentStatusEnum status:statuses) {
			combobox = new Combobox(status.getCode().toString(), status.getName());
			result.add(combobox);
		}
		return result;
	}

	@Override
	public void recordEquipmentStatus() {
		ShEquipmentType queryShEquipmentType = new ShEquipmentType();
		
		List<ShEquipmentDevIpc> ipcs = shEquipmentDevIpcService.selectEntryList(new ShEquipmentDevIpc());
		int ipcsLength = ipcs.size();
		queryShEquipmentType.setEquipmentTypeCode(EquipmentTypeEnum.DEV_IPC.getCode());
		Long ipcTypeId = shEquipmentTypeService.selectEntryList(queryShEquipmentType).get(0).getId();
		
		List<ShEquipmentDevTalk> talks = shEquipmentDevTalkService.selectEntryList(new ShEquipmentDevTalk());
		int talksLength = talks.size();
		queryShEquipmentType.setEquipmentTypeCode(EquipmentTypeEnum.DEV_TALK.getCode());
		Long talkTypeId = shEquipmentTypeService.selectEntryList(queryShEquipmentType).get(0).getId();
		
		List<ShEquipmentDevSwing> swings = shEquipmentDevSwingService.selectEntryList(new ShEquipmentDevSwing());
		int swingsLength = swings.size();
		queryShEquipmentType.setEquipmentTypeCode(EquipmentTypeEnum.DEV_SWING.getCode());
		Long swingTypeId = shEquipmentTypeService.selectEntryList(queryShEquipmentType).get(0).getId();
		
		List<ShEquipmentDevCamera> cameras = shEquipmentDevCameraService.selectEntryList(new ShEquipmentDevCamera());
		int camerasLength = cameras.size();
		queryShEquipmentType.setEquipmentTypeCode(EquipmentTypeEnum.DEV_CAMERA.getCode());
		Long cameraTypeId = shEquipmentTypeService.selectEntryList(queryShEquipmentType).get(0).getId();
		
		
		ShEquipmentStatus[] status = new ShEquipmentStatus[ipcsLength + talksLength + swingsLength + camerasLength];
		int count = 0;
		int devCount = 0;
		while(count < ipcsLength || count < talksLength || count < swingsLength || count < camerasLength) {
			
			//工控机
			if(count < ipcsLength) {
				devCount = count;
				status[devCount] = new ShEquipmentStatus();
				status[devCount].setEquipmentDevId(ipcs.get(count).getId());
				status[devCount].setEquipmentTypeId(ipcTypeId);
				status[devCount].setGmtCreate(new Date());
				if(EquipmentStatusEnum.PAUSE.getCode().equals(ipcs.get(count).getStatus())) {//暂停
					status[devCount].setStatus(EquipmentStatusEnum.PAUSE.getCode());
				}else {
					//待使用工控机验证接口
				}
			}
			
			//门口机
			if(count < talksLength) {
				devCount = count + ipcsLength;
				status[devCount] = new ShEquipmentStatus();
				status[devCount].setEquipmentDevId(talks.get(count).getId());
				status[devCount].setEquipmentTypeId(talkTypeId);
				status[devCount].setGmtCreate(new Date());
				if(EquipmentStatusEnum.PAUSE.getCode().equals(talks.get(count).getStatus())) {//暂停
					status[devCount].setStatus(EquipmentStatusEnum.PAUSE.getCode());
				}else {
					boolean isLive = IPAliveUtils.testIpAndPort(talks.get(count).getInternetIp(), talks.get(count).getInternetPort());
					if(isLive) {
						status[devCount].setStatus(EquipmentStatusEnum.AVAILABLE.getCode());
						talks.get(count).setStatus(EquipmentStatusEnum.AVAILABLE.getCode());
					}else {
						status[devCount].setStatus(EquipmentStatusEnum.NOAVAILABLE.getCode());
						talks.get(count).setStatus(EquipmentStatusEnum.NOAVAILABLE.getCode());
					}
					talks.get(count).setGmtModify(new Date());
					shEquipmentDevTalkService.updateById(talks.get(count));
				}
			}
			
			//摆闸
			if(count < swingsLength) {
				devCount = count + ipcsLength + talksLength;
				status[devCount] = new ShEquipmentStatus();
				status[devCount].setEquipmentDevId(swings.get(count).getId());
				status[devCount].setEquipmentTypeId(swingTypeId);
				status[devCount].setGmtCreate(new Date());
				if(EquipmentStatusEnum.PAUSE.getCode().equals(swings.get(count).getStatus())) {//暂停
					status[devCount].setStatus(EquipmentStatusEnum.PAUSE.getCode());
				}else {
					ShEquipmentDevSwing swing = swings.get(count);
					boolean isLive = IPAliveUtils.testIpAndPort(swing.getIpcIp(), swing.getIpcPort());
					if(isLive) {
						status[devCount].setStatus(EquipmentStatusEnum.AVAILABLE.getCode());
						swings.get(count).setStatus(EquipmentStatusEnum.AVAILABLE.getCode());
					}else {
						status[devCount].setStatus(EquipmentStatusEnum.NOAVAILABLE.getCode());
						swings.get(count).setStatus(EquipmentStatusEnum.NOAVAILABLE.getCode());
					}
					swings.get(count).setGmtModify(new Date());
					shEquipmentDevSwingService.updateById(swings.get(count));
				}
			}
			
			//监控
			if(count < camerasLength) {
				devCount = count + ipcsLength + talksLength + swingsLength;
				status[devCount] = new ShEquipmentStatus();
				status[devCount].setEquipmentDevId(cameras.get(count).getId());
				status[devCount].setEquipmentTypeId(cameraTypeId);
				status[devCount].setGmtCreate(new Date());
				status[devCount].setStatus(cameras.get(count).getStatus());
			}
			
			count++;
		}
		shEquipmentStatusService.insert(status);
	}

	

}
